/*
 * @Copyright: Copyright (c) 2019
 * @Author: caizhijun
 * @Version: 1.0
 * @Date: 2018-06-27 17:22:34
 */
var star=require("star");
cc.Class({
    extends: cc.Component,
    properties: {
       starPrefab:{
           default:null,
           type:cc.Prefab
       },
       scorePrefab:{
           default:null,
           type:cc.Prefab
       },
       maxStarDuration:0,
       minStarDuration:0,
       ground:{
           default:null,
           type:cc.Node
       },
       player:{
           default:null,
           type:cc.Node
       },
       scoreDisplay:{
           default:null,
           type:cc.Label
       },
       scoreAudio:{
           default:null,
           type:cc.AudioClip
       },
       startBtnNode:{
           default:null,
           type:cc.Node
       },
       exitBtnNode:{
           default:null,
           type:cc.Node
       },
       gameoverNode:{
           default:null,
           type:cc.Node
       },
       rockerNode:{
           default:null,
           type:cc.Node
       }
    },
    onLoad:function(){
        this.isRunningNow=false;
        this.groundY=this.ground.y+this.ground.height;
       // this.groundY=this.ground.y;
        this.timer=0;
        this.starDuration=0;
        this.starPool=new cc.NodePool("star");
        this.scorePool=new cc.NodePool("scoreFX");
    },
    onStartGame:function(){
        this.isRunningNow=true;
        this.score=0;
        this.scoreDisplay.string="Score:"+this.score.toString();
        this.startBtnNode.x = 3000;// this.startBtnNode.setPositionX(3000);
        this.exitBtnNode.x = 3000;// this.exitBtnNode.setPositionX(3000);
        this.gameoverNode.active=false;
        this.rockerNode.active=true;
        this.player.getComponent("player").startMoveAt(cc.v2(0,this.groundY));
       // this.player.getComponent("player").startMoveAt(cc.v2(960,this.groundY));
       // console.log(this.groundY);
        this.spawnNewStar();
    },
    onExitGame:function(){
        cc.director.end();
    },
    spawnNewStar:function(){
        var newstar;
        if(this.starPool.size()>0){
            newstar=this.starPool.get(this);
        }else{
            newstar=cc.instantiate(this.starPrefab);
        }
        this.node.addChild(newstar);
        newstar.setPosition(this.getNewStarPosition());
        newstar.getComponent("star").game=this;
        this.starDuration=this.minStarDuration+cc.random0To1()*(this.maxStarDuration-this.minStarDuration)+2;
        this.timer=0;
        this.currentStar=newstar;
    },
    getNewStarPosition:function(){
        var randX = 0;
        // var randY = this.groundY+cc.random0To1() * this.player.getComponent("player").jumpHeight + 199;
        var randY = this.groundY+cc.random0To1() *300+188;
        var maxX = this.node.width/2;
        randX = (Math.random() - 0.5) * 2 * maxX;// cc.randomMinus1To1() * maxX;
        return cc.v2(randX,randY);
    },
    despawnStar(star){
        this.starPool.put(star);
        this.spawnNewStar();
    },
    gainScore:function(pos){
        var newScore;
        //cc.log("8888888");
        if(this.scorePool.size()>0){
            newScore=this.scorePool.get(this);
        }else{
            newScore = cc.instantiate(this.scorePrefab);
            newScore.getComponent("score").init(this)
        }
        this.node.addChild(newScore);
        newScore.setPosition(pos);
        newScore.getComponent("score").play();
        this.score+=1;
        this.scoreDisplay.string="Score:"+this.score.toString();
        cc.audioEngine.playEffect(this.scoreAudio,false);//url,loop
    },
    update:function(dt){
        if(!this.isRunningNow) return;
       
        if(this.timer>this.starDuration){
            this.gameOver();
            return;
        }
        this.timer+=dt;
    },
    gameOver:function(){
        this.gameoverNode.active=true;
        this.rockerNode.active=false;
        this.player.getComponent("player").stopMove();
        this.rockerNode.getChildByName("rockerbar").setPosition(cc.v2(0,0));
        this.startBtnNode.x = 0;// this.startBtnNode.setPositionX(0);
        this.exitBtnNode.x = 0;// this.exitBtnNode.setPositionX(0);
        // this.currentStar.destroy();
        cc.log(this.currentStar);
        this.currentStar.removeFromParent();
        this.isRunningNow=false;
    }
});
